// 格式化时间
export const formatDateTime = (timeStr) => {
  const arr = timeStr.split('-')
  const date = new Date()
  const year = date.getFullYear()
  if (year === parseInt(arr[0])) {
    return `${arr[1]}-${arr[2]}`
  } else {
    return timeStr
  }
}

// 防抖(多次触发只执行最后一次)
export const debounce = (fn, delay) => {
  let timer = null
  let that = this
  return (...args) => {
    timer && clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(that, args)
    }, delay)
  }
}

// 节流(规定时间内只触发一次)
export const throttle = (fn, delay) => {
  let timer = null
  let that = this
  return (...args) => {
    if (timer) return
    timer = setTimeout(() => {
      fn.apply(that, args)
      timer = null
    }, delay)
  }
}
